/** */
package co.com.umobile.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import co.com.umobile.application.GlobalState;
import co.com.umobile.domain.User;
import co.com.umobile.persistence.DatabaseManager;

/**
 * @author sergiogranada
 * 
 */
public class UserSessionDAO {

	/**
	 * 
	 */
	private UserSessionDAO() {
	}

	/**
	 * updates the user data.
	 * 
	 * @param userSession
	 *            userSession.
	 * @param context
	 *            context.
	 */
	public static void updateUser(User userSession, Context context) {

		DatabaseManager.getDatabase().delete("User", "userCode=?", new String[] { userSession.getUserCode() });

		ContentValues values = new ContentValues();
		values.put("userCode", userSession.getUserCode());
		values.put("password", userSession.getPassword());
		values.put("name", userSession.getName());
		values.put("sessionId", userSession.getSessionId());
		values.put("userType", userSession.getUserType());
		values.put("userTypeDescription", userSession.getUserTypeDescription());

		DatabaseManager.getDatabase().insert("User", null, values);

		// Update GlobalState with the last user session
		((GlobalState) context.getApplicationContext()).setLastUserSession(userSession);

	}

	/**
	 * gets the user profile.
	 * 
	 * @param userName
	 *            userName.
	 * @param company
	 *            company.
	 * @return UserSession
	 */
	public static User getUserSession(String userCode) {

		Cursor cursor = DatabaseManager.getDatabase().rawQuery("select * from User where userCode=?",
				new String[] { userCode });

		return validateCursorResult(cursor);

	}

	/**
	 * gets the user profile.
	 * 
	 * @param userName
	 *            userName.
	 * @param company
	 *            company.
	 * @return UserSession
	 */
	public static User getLastUserSession() {

		Cursor cursor = DatabaseManager.getDatabase().rawQuery("select * from User where userCode=? Limit 1", null);

		return validateCursorResult(cursor);

	}

	/**
	 * validate cursor result and return the user session.
	 * 
	 * @param cursor
	 *            cursor.
	 * @return UserSession
	 */
	private static User validateCursorResult(Cursor cursor) {
		if (!cursor.moveToFirst()) {
			cursor.close();
			return null;
		}

		User userSession = createUserSessionByCursor(cursor);

		cursor.close();
		return userSession;
	}

	/**
	 * create a user session by cursor information.
	 * 
	 * @param cursor
	 *            cursor
	 * @return UserSession
	 */
	private static User createUserSessionByCursor(Cursor cursor) {
		User userSession = new User();

		userSession.setUserCode(cursor.getString(cursor.getColumnIndexOrThrow("userCode")));
		userSession.setPassword(cursor.getString(cursor.getColumnIndexOrThrow("password")));
		userSession.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
		userSession.setSessionId(cursor.getString(cursor.getColumnIndexOrThrow("sessionId")));
		userSession.setUserType(cursor.getInt(cursor.getColumnIndexOrThrow("userType")));
		userSession.setUserTypeDescription(cursor.getString(cursor.getColumnIndexOrThrow("userTypeDescription")));

		return userSession;
	}

}
